package com.example.design_pattern.iterator.inner;

import com.example.design_pattern.iterator.Collection;
import com.example.design_pattern.iterator.Iterator;

import java.util.ArrayList;
import java.util.List;

/**
 * 不对外提供get方法，只能通过迭代器进行访问
 * @param <E>
 */
public class MyCollectionIn<E> extends Collection<E> {

    private List<E> mList;

    public MyCollectionIn(){
        mList = new ArrayList<>();
    }

    public void add(E e){
        mList.add(e);
    }

    public int size(){
        return mList.size();
    }

    @Override
    public Iterator<E> createIterator() {
        return new MyIteratorIn();
    }


    /**
     * 内部的迭代器
     */
    private class MyIteratorIn implements Iterator<E>{

        private int index = -1;

        @Override
        public boolean hasNext() {
            return index < size() - 1;
        }

        @Override
        public E next() {
            index++;
            return mList.get(index);
        }
    }
}
